package com.lxiaodd.service;

import java.util.Arrays;
import java.util.List;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;

public class OutService {
	public static String TABLENAME="out";
	
	/**
	 * 保存（微信消息）
	 * @param openid
	 * @param type
	 * @param money
	 * @param msgId
	 * @param createTime
	 * @return
	 */
	public String save(String openid,String type,double money,Long msgId,Long createTime,String outDate){
		List<AVObject> list=findByMsgIdAndCreateTime(msgId,createTime,openid);
		if(list==null || list.size()<=0){
			AVObject out=new AVObject(TABLENAME);
			out.put("openid", openid);
			out.put("type", type);
			out.put("money", money);
			out.put("msgId", msgId);
			out.put("createTime",createTime);
			out.put("outDate",outDate);
			try {
				out.save();
				return out.getObjectId();
			} catch (AVException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}else{
			return "1";
		}
	}
	
	/**
	 * 保存
	 * @param openid
	 * @param type
	 * @param money
	 * @return
	 */
	public String save(String openid,String type,double money,String outDate){
		AVObject out=new AVObject(TABLENAME);
		out.put("openid", openid);
		out.put("type", type);
		out.put("money", money);
		out.put("outDate",outDate);
		try {
			out.save();
			return out.getObjectId();
		} catch (AVException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 通过msgId，createTime 查询 ，避免重复数据
	 * @param msgId
	 * @param createTime
	 * @return
	 */
	public List<AVObject> findByMsgIdAndCreateTime(Long msgId,Long createTime,String openid){
		AVQuery<AVObject> msgIdQuery = new AVQuery<>(TABLENAME);
		msgIdQuery.whereEqualTo("msgId", msgId);
	    AVQuery<AVObject> createTimeQuery = new AVQuery<>(TABLENAME);
	    createTimeQuery.whereEqualTo("createTime",createTime);
	    AVQuery<AVObject> openidQuery = new AVQuery<>(TABLENAME);
	    openidQuery.whereEqualTo("openid",openid);
	    	    
	    AVQuery<AVObject> query = AVQuery.and(Arrays.asList(msgIdQuery, createTimeQuery,openidQuery));
	    try {
			List<AVObject> list = query.find();
			return list;
		} catch (AVException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 通过时间查询
	 * @param outDate
	 * @return
	 */
	public List<AVObject> findByOutDate(String outDate,String openid){
		AVQuery<AVObject> dateQuery = new AVQuery<>(TABLENAME);
		dateQuery.whereEqualTo("outDate", outDate);
		AVQuery<AVObject> openidQuery = new AVQuery<>(TABLENAME);
	    openidQuery.whereEqualTo("openid",openid);	    
	    AVQuery<AVObject> query = AVQuery.and(Arrays.asList(dateQuery,openidQuery));
	    try {
			List<AVObject> list = query.find();
			return list;
		} catch (AVException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 返回给定日期消费总金额
	 * @param outDate
	 * @param openid
	 * @return
	 */
	public Double getSumMoneyByDate(String outDate,String openid){
		List<AVObject> list=findByOutDate(outDate,openid);
		if(list!=null && list.size()>0){
			double sum=0d;
			for(AVObject obj:list){
				sum+=obj.getNumber("money").doubleValue();
			}
			return sum;
		}else{
			return 0d;
		}
	}
}
